//
//  SSSMerchantBenefitSearchDetailController.m
//  SSSMerchant
//
//  Created by xnxin on 2017/3/24.
//  Copyright (c) 2017 xnxin. All rights reserved.
//

#import "SSSMerchantBenefitSearchDetailController.h"
#import "SSSMerchantSearchViewCell.h"
#import "SSSMerchantSearchViewCellModel.h"
#import "NSNumber+Addition.h"
#import "SSSMerchantTodayBenefitViewCellModel.h"
#import "APITool.h"
#import "NSDictionaryAdditions.h"
#import "NSDateAddition.h"
#import "XHTableViewSection.h"

@interface SSSMerchantBenefitSearchDetailController ()

@property (nonatomic, strong) UIView *headerView;

///当日收益总额
@property (nonatomic, strong) NSDecimalNumber *totalMoney;
@property (nonatomic, strong) UILabel *totalMoneyLabel;
@end

@implementation SSSMerchantBenefitSearchDetailController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"收益查询";
    self.tableView.rowHeight = 75;
    self.tableView.left = 15;
    self.tableView.width = DEVICE_WIDTH - 30;
    self.tableView.backgroundColor = self.view.backgroundColor;

    self.tableView.tableHeaderView = self.headerView;
    self.totalMoney = [NSDecimalNumber decimalNumberWithString:@"0"];
    self.tableView.allowsSelection = NO;

    if (self.chooseDate != nil) {

        self.networkHelper.params[@"queryDate"] = [self.chooseDate xhstringWithFormat:@"yyyy-MM-dd"];
        [self.tableView.mj_header beginRefreshing];
    }
}

#pragma mark -

- (BOOL)autobeninRefresh {
    return false;
}
- (UIView *)headerView {

    if(!_headerView) {

        _headerView = [UIView new];
        _headerView.backgroundColor = self.view.backgroundColor;
        _headerView.height = 78;
        _headerView.width = self.tableView.width;

        UILabel *tipsLabel = [UILabel new];
        tipsLabel.text = @"当日收益";
        tipsLabel.font = [UIFont systemFontOfSize:13];
        tipsLabel.textColor = [UIColor colorWithHex:@"57C18B"];
        [tipsLabel sizeToFit];

        [_headerView addSubview:tipsLabel];
        tipsLabel.centerX = self.tableView.width/2;
        tipsLabel.top = 15;

        _totalMoneyLabel = [UILabel new];
        _totalMoneyLabel.font = [UIFont systemFontOfSize:14];
        _totalMoneyLabel.textColor = [UIColor colorWithHex:@"57C18B"];
        [_headerView addSubview:_totalMoneyLabel];
    }
    return _headerView;
}

- (NSString *)url {
    return [APITool urlStr:@"/bApi/myIncome/dayIncome"];
}

- (void)setTotalMoney:(NSDecimalNumber *)totalMoney {
    _totalMoney = totalMoney;

    self.totalMoneyLabel.text = [NSString stringWithFormat:@"总额：%@元", totalMoney.moneyValue ?: @"0.00"];
    [self.totalMoneyLabel sizeToFit];
    self.totalMoneyLabel.centerX = self.tableView.width/2;
    self.totalMoneyLabel.top = 40;
}

- (NSArray<NSString *> *)nibNameArray {
    return @[@"SSSMerchantTodayBenefitViewCell"];
}

- (NSArray*)processData:(NSDictionary *)data {

    self.totalMoney = [NSDecimalNumber decimalNumberWithString:[[data objectOrNilForKey:@"totalIncome"] description]];
    NSArray *modelArray = [NSArray modelArrayWithClass:[SSSMerchantTodayBenefitViewCellModel class]
                                                  json:[[data objectOrNilForKey:@"page"] objectOrNilForKey:@"pageList"]];
    return modelArray;
}

- (void)setChooseDate:(NSDate *)chooseDate {
    _chooseDate = chooseDate;
    self.networkHelper.params[@"queryDate"] = [chooseDate xhstringWithFormat:@"yyyy-MM-dd"];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(XHBaseTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([self.dataSource isLastCellForIndexPath:indexPath]) {
        cell.hidenGrayLine = YES;
    }
}
@end
